﻿using MediatR;
using MS.BasicData.Domain;
using MS.BasicData.Infrastructure;
using MS.BasicData.Infrastructure.IBaseRepository;
using System.ComponentModel.DataAnnotations;

namespace MS.BasicData.Read.API.Applications.Permissionss
{
    public class DeletedRequestHandler : IRequestHandler<DeletedRequestModel, ApiRestult>
    {
        private readonly IPermisBaseBaseRepository _permissionsRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="permissionsRepository"></param>
        public DeletedRequestHandler(IPermisBaseBaseRepository permissionsRepository)
        {
            _permissionsRepository = permissionsRepository;
        }

        public async Task<ApiRestult> Handle(DeletedRequestModel request, CancellationToken cancellationToken)
        {
            ApiRestult restult = new ApiRestult
            {
                isSuc = false,
                Message = "权限信息删除成功",
                Code = Restult.Success
            };
            var permissin = await _permissionsRepository.GetAll(a => a.PermissionId == request.PermissionsId);
            if (permissin == null)
            {
                restult.isSuc = true;
                restult.Message = "权限信息不存在";
                restult.Code = Restult.Fail;
                return restult;
            }
            permissin.IsDeleted = true;
            await _permissionsRepository.UpdateAsync(permissin);
            return restult;
        }
    }


    public class DeletedRequestModel : IRequest<ApiRestult>
    {
        /// <summary>
        /// 权限Id
        /// </summary>
        public int PermissionsId { get; set; }


    }
}
